package mp3seller.daos;

import java.util.HashMap;

import mp3seller.entities.Consumer;
import mp3seller.entities.Mp3;
import mp3seller.util.UserInfo;
import mp3seller.util.Util;

/**
 * Classe de persistencia padrao DAO para clientes do sistema
 * @author Raoni
 *
 */
public class ConsumerDAO {	
	
	private HashMap<Long, Consumer> consumers = new HashMap<Long, Consumer>();
	/**
	 * Comprar mp3
	 * @param consumer
	 * @param mp3
	 */
	public void buyMp3(Consumer consumer, Mp3 mp3){
		consumer.addMp3Owned(mp3);
	}
	/**
	 * salvar Cliente
	 * @param consumer
	 */
	public void saveConsumer(Consumer consumer){
		consumer.setId(Util.nextCode(consumers.keySet()));
		consumers.put(consumer.getId(), consumer);
	}
	/**
	 * deletar Cliente
	 * @param consumer
	 */
	public void deleteConsumer(Consumer consumer){
		consumers.remove(consumer.getId());
	}	
	/**
	 * deletar consumer
	 * @param id
	 */
	public void deleteConsumer(long id) {
		consumers.remove(getConsumer(id).getId());
	}
	/**
	 * atualizar Cliente
	 * @param consumer
	 */
	public void updateConsumer(Consumer consumer){
		consumers.put(consumer.getId(), consumer);
		UserInfo.setUser(consumer);
		System.out.println(getConsumer(consumer.getId()));
	}
	/**
	 * pegar cliente
	 * @param id
	 * @return
	 */
	public Consumer getConsumer(long id){
		return consumers.get(id);
	}
	/**
	 * pegar Cliente pleo login
	 * @param login
	 * @return
	 */
	public Consumer getConsumer(String login){
		for(Consumer consumer : consumers.values()){
			if(consumer.getLogin().equalsIgnoreCase(login)){
				return consumer;
			}
		}
		return null;
	}
	
	public Consumer getConsumer(String login, String pass){
		Consumer consumer = getConsumer(login);
		if(consumer != null && consumer.getPassword().equalsIgnoreCase(pass)){
			return consumer;
		}
		
		return null;
	}
	/**
	 * pegar todos os Clientes
	 * @return
	 */
	public HashMap<Long, Consumer> getConsumer(){
		return consumers;
	}
	
	
}
